package com.mybase.app.app;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.mybase.app.IO_Net.IOHomeActivity;
import com.mybase.app.Resource.ResourceHomeActivity;
import com.mybase.app.UI.UIHomeActivity;
import com.mybase.app.advancedBase.AdvanceHomeActivity;
import com.mybase.app.extend.ExtendHomeActivity;
import com.mybase.app.fourBase.FourHomeActivity;
import com.mybase.app.entity.MyMenuItem;
import com.mybase.app.system.SystemHomeActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by happy_000 on 2014/11/20.
 */
public class HomeActivity extends ListActivity {
    private ListAdapter mAdapter;
    List<MyMenuItem> itemList=new ArrayList<MyMenuItem>(0);
    private List<String> list=new ArrayList<String>(0);


    public  List<MyMenuItem> getRootMenuItems() {
        List<MyMenuItem> list=new ArrayList<MyMenuItem>(0);
        MyMenuItem menuItem1 = new MyMenuItem();
        menuItem1.setName("Base 四大组件及相关");
        menuItem1.setActivity(FourHomeActivity.class);
        list.add(menuItem1);
        //**********************************************************
        MyMenuItem menuItem2 = new MyMenuItem();
        menuItem2.setName("UI 控件布局");
        menuItem2.setActivity(UIHomeActivity.class);
        list.add(menuItem2);
        //**********************************************************
        MyMenuItem menuItem3 = new MyMenuItem();
        menuItem3.setName("System 系统信息");
        menuItem3.setActivity(SystemHomeActivity.class);
        list.add(menuItem3);
        //**********************************************************
        MyMenuItem menuItem4 = new MyMenuItem();
        menuItem4.setName("extend 第三方扩展");
        menuItem4.setActivity(ExtendHomeActivity.class);
        list.add(menuItem4);
        //**********************************************************
        MyMenuItem menuItem5 = new MyMenuItem();
        menuItem5.setName("Resource  资源文件");
        menuItem5.setActivity(ResourceHomeActivity.class);
        list.add(menuItem5);
        //**********************************************************
        MyMenuItem menuItem6 = new MyMenuItem();
        menuItem6.setName("IO_net 输入出流");
        menuItem6.setActivity(IOHomeActivity.class);
        list.add(menuItem6);
        //**********************************************************
        MyMenuItem menuItem7 = new MyMenuItem();
        menuItem7.setName("advance  功能模块");
        menuItem7.setActivity(AdvanceHomeActivity.class);
        list.add(menuItem7);
        //**********************************************************
        MyMenuItem menuItem8 = new MyMenuItem();
        menuItem8.setName("minute  记要");
        menuItem8.setActivity(AdvanceHomeActivity.class);
        list.add(menuItem8);
        //**********************************************************

        return list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        itemList= getRootMenuItems();
        initdatas();
        mAdapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,list);
        setListAdapter(mAdapter);
    }
    private  void initdatas(){
        for (MyMenuItem myMenuItem : itemList) {
            list.add(myMenuItem.getName());
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        MyMenuItem myMenuItem=itemList.get(position);
        Intent intent=new Intent(HomeActivity.this,myMenuItem.getActivity());
        startActivityForResult(intent,RESULT_OK);
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定要退出吗?");
        builder.setTitle("提示");
        builder.setPositiveButton("确认",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        finish();
                        //System.exit(1);
//                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }

    private void onExit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定要注销吗?");
        builder.setTitle("提示");
        builder.setNeutralButton("注销", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent logoutIntent = new Intent(HomeActivity.this, LoginActivity.class);
                logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(logoutIntent);
                HomeActivity.this.finish();
            }
        });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }
}
